use crate::{bootstrap::AppState, middleware::jwt::user::Claims, router::resp::RespVO, service};
use axum::{body::Body, extract::State, http::Response, response::IntoResponse, Json};
use sky_pojo::{dto::user::UserLoginDTO, vo::user::UserLoginVO};

pub async fn login(State(state): State<AppState>, Json(dto): Json<UserLoginDTO>) -> Response<Body> {
    tracing::info!("用户登录：{:?}", dto.code);

    let result = service::user::wechat_login(state.db_pool, dto).await;

    match result {
        Ok(user) => {
            let _token = Claims::new(Claims {
                id: user.id,
                name: user.name.unwrap_or_default(),
                exp: None,
                sub: None,
            })
            .unwrap();

            let _vo = UserLoginVO {
                id: user.id,
                openid: user.openid.unwrap().clone(),
                token: _token,
            };

            return RespVO::<UserLoginVO>::success("登录成功!".to_owned(), Some(_vo))
                .into_response();
        }

        Err(msg) => RespVO::<()>::error_without_data(msg).into_response(),
    }
}

pub async fn get_cuerrent_info(State(_state): State<AppState>) -> Response<Body> {
    RespVO::<()>::error_without_data(format!("token_test")).into_response()
}
